package com.zz.shop.web.controller;

import com.alibaba.fastjson.JSON;
import com.zz.shop.common.Const;
import com.zz.shop.common.ResponseCode;
import com.zz.shop.common.ResponseResult;
import com.zz.shop.service.UserService;
import com.zz.shop.service.impl.UserServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/sys")
public class SystemController extends BaseServlet{
    private UserService us=new UserServiceImpl();
    public String activate(HttpServletRequest request, HttpServletResponse response){
        String e = request.getParameter("e");
        String c = request.getParameter("c");
        ResponseResult rr=us.activate(e,c);
        request.setAttribute("msg",rr);
        return Const.MESSAGE;
    }

    public String sendMessage(HttpServletRequest request, HttpServletResponse response){
        String phone = request.getParameter("phone");
        System.out.println(phone);
        ResponseResult rr=us.sendMessage(phone);
        if (rr.getCode()== ResponseCode.SUCCESS.getCode()) {
            request.getSession().setAttribute(Const.MESSAGE_CODE,rr.getMsg());
        }
        String s = JSON.toJSONString(rr);
        return s;
    }

    public String checkCode(HttpServletRequest request, HttpServletResponse response){
        System.out.println("22");
        String code = request.getParameter("code");
        String messageCode = (String) request.getSession().getAttribute(Const.MESSAGE_CODE);
        if (messageCode.equals(code)) {
            return JSON.toJSONString(ResponseResult.createSuccess("验证码正确"));
        }
        return JSON.toJSONString(ResponseResult.createSuccess("验证码错误"));

    }
}
